Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
babel-plugin-transform-regenerator
Advanced tools
Explode async and generator functions into a state machine.
The babel-plugin-transform-regenerator npm package is used to transform ES2015+ generator functions into ES5 code. This is particularly useful for writing asynchronous code in a synchronous manner using the `yield` keyword, and for ensuring compatibility with older browsers or environments that do not support generator functions natively.
Transformation of generator functions
This feature allows developers to write generator functions using the `function*` syntax and `yield` keyword. The babel-plugin-transform-regenerator will then transform this into ES5 compatible code, enabling the use of generator functions in environments that do not support them natively.
function* gen() { yield 'Hello'; yield 'World'; }
Async/Await support
Although primarily focused on generator functions, babel-plugin-transform-regenerator also plays a crucial role in enabling the use of async/await syntax in projects that use Babel for transpilation. It transforms async functions into generator functions and then into ES5 code.
async function foo() { await bar(); }
This package transforms async functions into generator functions, similar to how babel-plugin-transform-regenerator handles async/await syntax. However, it is specifically focused on the async/await syntax rather than on generator functions in general.
While not a Babel plugin, regenerator-runtime is often used in conjunction with babel-plugin-transform-regenerator to provide a runtime environment for the transformed generator functions. It includes a polyfill for the regenerator runtime, which is necessary for the transpiled generator functions to work.
Transform async/generator functions with regenerator
In
function* a() {
yield 1;
}
Out
var _marked = [a].map(regeneratorRuntime.mark);
function a() {
return regeneratorRuntime.wrap(function a$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return 1;
case 2:
case "end":
return _context.stop();
}
}
}, _marked[0], this);
}
npm install --save-dev babel-plugin-transform-regenerator
.babelrc
(Recommended)Without options:
{
"plugins": ["transform-regenerator"]
}
With options:
name | default value |
---|---|
asyncGenerators | true |
generators | true |
async | true |
{
"plugins": [
["transform-regenerator", {
"asyncGenerators": false,
"generators": false,
"async": false
}]
]
}
babel --plugins transform-regenerator script.js
require("babel-core").transform("code", {
plugins: ["transform-regenerator"]
});
FAQs
Explode async and generator functions into a state machine.
The npm package babel-plugin-transform-regenerator receives a total of 1,056,007 weekly downloads. As such, babel-plugin-transform-regenerator popularity was classified as popular.
We found that babel-plugin-transform-regenerator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.